<?php

class categories{
	
	function getParentCategoryList(){
		$categoryList = array();
		qdbconn();
		
		$query = "SELECT * FROM categories
					WHERE cat_level = 1";
		$result = mysql_query($query);
		
		$i = 0;
		while ($row = mysql_fetch_row($result)) {
			$categoryList[$i]['cat_id']	= $row[0];
			$categoryList[$i]['cat_name']= $row[1];
			$categoryList[$i]['cat_status']= $row[2];						
			$i++;
		}
		return $categoryList;
	}
	
	function getCategoriesList(){
		$categoryList = array();
		qdbconn();
		
		$query = "SELECT * FROM categories";
		$result = mysql_query($query);
		
		$i = 0;
		while ($row = mysql_fetch_row($result)) {
			$categoryList[$i]['cat_id']	= $row[0];
			$categoryList[$i]['cat_name']= $row[1];
			$categoryList[$i]['cat_status']= $row[2];
			$i++;
		}
		return $categoryList;
	}
	
	function getCategoriesLevel1List()
	{
		$categoryList = array();
		qdbconn();
		
		$query = "SELECT * FROM categories
					WHERE cat_level=1";
		$result = mysql_query($query);
		
		$i = 0;
		while ($row = mysql_fetch_row($result)) {
			$categoryList[$i]['cat_id']	= $row[0];
			$categoryList[$i]['cat_name']= $row[1];
			$categoryList[$i]['cat_status']= $row[2];					
			$categoryList[$i]['cat_sub_cate']= $this->getSubCateList($row[0]);			
			$i++;
		}
		return $categoryList;
	}
	
	function getSubCateList($cate_parent_id){
		$categoryList = array();
		qdbconn();
		
		$query = "SELECT * FROM categories
					WHERE cat_cat_id=$cate_parent_id";
		$result = mysql_query($query);		
		
		$i = 0;
		while ($row = mysql_fetch_row($result)) {
			$categoryList[$i]['cat_id']	= $row[0];
			$categoryList[$i]['cat_name']= $row[1];
			$categoryList[$i]['cat_status']= $row[2];
			$i++;
		}
		return $categoryList;
	}
	
	function inactivateACategory($cat_id){
		qdbconn();		
		$query = "UPDATE categories SET cat_status = 0 where cat_id = $cat_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function activateACategory($cat_id){
		qdbconn();		
		$query = "UPDATE categories SET cat_status = 1 where cat_id = $cat_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function editACategory($data){
		qdbconn();
		$cat_name 		= $data['cat_name'];
		$cat_id 		= $data['cat_id'];
		
		$query = "SELECT count(1) FROM categories
					WHERE cat_name = '$cat_name'";
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			if ($row[0]==0) //cat_name khong bi trung
			{
				$query = "UPDATE categories SET cat_name='$cat_name'
							WHERE cat_id = $cat_id";
				
				$result = mysql_query($query);
				return $result;
			}
			else 
			{
				return -1; //duplicated
			}
		}			
	}
	
	function insertACategory($data){
		qdbconn();
		$cat_name = $data['cat_name'];
		
		$cat_cat_id = $data['cat_cat_id'];
		
		if ($cat_cat_id!=0) {
			$cat_level = 2;
		}
		else $cat_level = 1;
		
		$query = "SELECT count(1) FROM categories
					WHERE cat_name = '$cat_name'";
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			if ($row[0]==0) 
			{
				$query = "INSERT INTO categories (cat_name,cat_cat_id,cat_level) 
				values ('$cat_name',$cat_cat_id,$cat_level)";
				//echo $query;die;
				$result = mysql_query($query);
				return $result;
			}
			else 
			{
				return -1; //duplicated
			}
		}		
	}
	
	
	
	function getCategoriesInfo($cat_id){
		qdbconn();		
		$query = "SELECT * FROM categories
					WHERE cat_id = '$cat_id'";
		$result = mysql_query($query);
		$current_data = array();
		if ($row = mysql_fetch_row($result)) {
			$current_data['cat_id'] 		= $row[0];
			$current_data['cat_name'] 	= $row[1];
			$current_data['cat_status'] 	= $row[2];
			$current_data['cat_cat_id'] 	= $row[3];
			
			
		return $current_data;
	}
	
	}
}


?>